package com.itheima.project.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {

        http.formLogin()             //自定义自己编写的登陆页面
                .loginPage("/login.html")    //登录页面设置
                .loginProcessingUrl("/login") //登录访问路径
                .permitAll()//登录页和登录访问路径无需登录也可以访问
                .and()
                .authorizeRequests()
                .antMatchers("/css/**","/images/**").permitAll()
                .antMatchers("/hello/user").hasAnyRole("USER","ADMIN")
                .antMatchers("/hello/admin").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
                .csrf().disable();    //关闭csrf防护
        return http.build();
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
/*    @Bean
      public UserDetailsService userDetailsService(){
        UserDetails user = User.builder()
                .username("user")
                .password("$2a$10$c8v1RU3boVDYmM2o3VItp.JKfz0d7E.7RaglPmJtYN6HvNIvrqji2")
                .roles("USER")
                .build();
        UserDetails admin = User.builder()
                .username("admin")
                .password("e10adc3949ba59abbe56e057f20f883e")
                .roles("USER","ADMIN")
                .build();
        return new InMemoryUserDetailsManager(user,admin);
    }*/
}